package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {
    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private BlackListApi blackListApi;

    //查询通用设置
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        //查询当前用户ID
        Long userId = UserHolder.getUserId();
        vo.setId(userId);
        //获取当前用户手机号
        String phone = UserHolder.getMobile();
        vo.setPhone(phone);
        //获取陌生人问题
        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "你喜欢Java嘛" : question.getTxt();
        vo.setStrangerQuestion(txt);
        //获取用户得APP开关通知
        Settings settings = settingsApi.findByUserId(userId);
        if (settings != null) {
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setLikeNotification(settings.getLikeNotification());
        }
        return vo;
    }

    /**
     * 保存陌生人问题
     *
     * @param content
     */
    public void saveQuestion(String content) {
        //1、获取当前用户ID
        Long userId = UserHolder.getUserId();
        //2、调用api查询当前用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        //判断用户是否设置问题，设置了进行更新操作，没设置进行保存操作
        if (question == null) {
            //没有设置问题。进行保存操作
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            //有问题，则进行更新操作
            question.setTxt(content);
            questionApi.update(question);
        }
    }

    public void saveSettings(Map map) {
        boolean likeNotification = (Boolean) map.get("likeNotification");
        boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (Boolean)  map.get("gonggaoNotification");
        //1.获取当前用户ID
        Long userId = UserHolder.getUserId();
        //2.根据用户ID，查询用户得通用设置
        Settings settings = settingsApi.findByUserId(userId);
        //3.判断是否为空，从而进行不同得操作
        if (settings==null){
            //进行保存操作
             settings = new Settings();
            settings.setUserId(userId);
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.save(settings);
        }else {
            //进行更新操作
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.update(settings);
        }
    }

    /**
     * 分页查询黑名单
     * @param page
     * @param size
     * @return
     */
    public PageResult blacklist(int page, int size) {
        //1、获取当前用户的id
        Long userId = UserHolder.getUserId();
        //2、调用API查询用户的黑名单分页列表  Ipage对象
        IPage<UserInfo> userInfo = blackListApi.findByUserId(userId, page, size);
        //3、对象转化，将查询的Ipage对象的内容封装到PageResult中
        PageResult pageResult = new PageResult(page, size, Math.toIntExact(userInfo.getTotal()), userInfo.getRecords());
        //4、返回
        return pageResult;
    }

    /**
     * 取消黑名单
     * @param userId
     * @param blackUserId
     */
    public void delete(Long userId, Long blackUserId) {
        blackListApi.delete(userId,blackUserId);

    }
}
